package cn.tangsihai.hrm.service.impl;

import cn.tangsihai.hrm.client.AuthClient;
import cn.tangsihai.hrm.constant.Constants;
import cn.tangsihai.hrm.domain.Employee;
import cn.tangsihai.hrm.domain.Login;
import cn.tangsihai.hrm.domain.Tenant;
import cn.tangsihai.hrm.dto.LoginAndMealId;
import cn.tangsihai.hrm.dto.TenantDto;
import cn.tangsihai.hrm.error.ErrorCode;
import cn.tangsihai.hrm.mapper.EmployeeMapper;
import cn.tangsihai.hrm.mapper.TenantMapper;
import cn.tangsihai.hrm.service.ITenantService;
import cn.tangsihai.hrm.util.AjaxResult;
import cn.tangsihai.hrm.util.AssertUtil;
//import cn.tangsihai.hrm.web.controller.LoginController;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whale.tang
 * @since 2021-11-20
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
    @Autowired
    private AuthClient authClient;
    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private EmployeeMapper employeeMapper;
    /**
     * 机构入驻业务
     * @param tenantDto
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void entering(TenantDto tenantDto) {
        // 校验参数合理性
        Employee employee = tenantDto.getEmployee();
        Login login = tenantDto.getLogin();
        Long mealId = tenantDto.getMealId();
        Tenant tenant = tenantDto.getTenant();

        // 断言
        AssertUtil.isNotEmpty(String.valueOf(mealId), ErrorCode.MEAL_IS_NULL.getErrorMessage());
        AssertUtil.isNotEmpty(employee.getEmail(), ErrorCode.MAIL_IS_NULL.getErrorMessage());
        AssertUtil.isNotEmpty(employee.getTel(), ErrorCode.TEL_IS_NULL.getErrorMessage());

        //todo 校验数据
        //..............
        // 远程保存login、login_meal表
        LoginAndMealId loginAndMealId = new LoginAndMealId();
        loginAndMealId.setLogin(login);
        loginAndMealId.setMealId(mealId);
        AjaxResult ajaxResult = authClient.saveLoginAndMeal(loginAndMealId);

        if (!ajaxResult.isSuccess() || ajaxResult.getResultObj() == null){
            throw new RuntimeException("保存登录异常：" + ajaxResult.getMessage());
        }

        Object obj = ajaxResult.getResultObj();
        Long loginId = Long.valueOf(obj.toString());

        // 准备机构数据
        Tenant tenant1 = initTenant(tenant);
        // 保存数据，获取自增长id
        tenantMapper.insert(tenant1);

        // 准备员工表数据
        Employee employee1 = initEmployee(employee, tenant1, loginId);
        // 保存数据，获取自增长id
        employeeMapper.insert(employee1);

        // 获取自增长id更新机构数据
        tenant1.setAdminId(employee1.getId());
        tenantMapper.updateById(tenant1);
    }

    // 准备数据
    private Tenant initTenant(Tenant tenant) {
        Tenant tenant1 = new Tenant();
        BeanUtils.copyProperties(tenant, tenant1);
        tenant1.setRegisterTime(new Date());
        tenant1.setState(Constants.Tenant.state_wait_audit);
        return tenant1;
    }

    private Employee initEmployee(Employee employee, Tenant tenant1, Long loginId) {
        Employee employee1 = new Employee();
        employee1.setRealName(employee.getTel());
        employee1.setTenantId(tenant1.getId());
        employee1.setEmail(employee.getEmail());
        employee1.setTel(employee.getTel());
        employee1.setInputTime(tenant1.getRegisterTime());
        employee1.setState(Constants.Employee.state_normal);
        employee1.setType(Constants.Employee.type_tenant_service);
        employee1.setLoginId(loginId);
        return employee1;
    }
}
